//
//  LoginMenuViewController.m
//  TipsProject
//
//  Created by Suny on 15/7/9.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "LoginMenuViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "LoginViewController.h"
@interface LoginMenuViewController ()

@property (nonatomic,strong) UIImageView *backgroundView;

@property (nonatomic,strong) UIImageView *logoView;

@property (nonatomic,strong) UIButton *weiboLoginButton;

@property (nonatomic,strong) UIButton *weixinLoginButton;

@property (nonatomic,strong) UIButton *qqLoginButton;

@property (nonatomic,strong) UIButton *mailLoginButton;



@end

@implementation LoginMenuViewController
DEF_SINGLETON();

- (UIImageView *)backgroundView
{
    if (!_backgroundView)
    {
        _backgroundView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _backgroundView.image = [UIImage imageNamed:@"login_background"];
    }
    return _backgroundView;
}


- (UIImageView *)logoView
{
	if (!_logoView)
	{
		_logoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 112, 119)];
		_logoView.image = [UIImage imageNamed:@"logo_icon"];
		
	}
	return _logoView;
}

- (UIButton *)weiboLoginButton
{
	if (!_weiboLoginButton)
	{
		_weiboLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_weiboLoginButton.frame = CGRectMake(0, 0, 45, 77);
		[_weiboLoginButton setImage:[UIImage imageNamed:@"weibo_login_icon"] forState:UIControlStateNormal];
		[_weiboLoginButton addTarget:self action:@selector(loginWithButton:) forControlEvents:UIControlEventTouchUpInside];
		_weiboLoginButton.tag = 1;
	}
	
	return _weiboLoginButton;
}


- (UIButton *)weixinLoginButton
{
	if (!_weixinLoginButton)
	{
		_weixinLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_weixinLoginButton.frame = CGRectMake(0, 0, 45, 77);
		[_weixinLoginButton setImage:[UIImage imageNamed:@"weixin_login_icon"] forState:UIControlStateNormal];
		[_weixinLoginButton addTarget:self
							   action:@selector(loginWithButton:) 
					 forControlEvents:UIControlEventTouchUpInside];
		_weixinLoginButton.tag = 2;
	}
	
	return _weixinLoginButton;
}

- (UIButton *)qqLoginButton
{
	if (!_qqLoginButton)
	{
		_qqLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_qqLoginButton.frame = CGRectMake(0, 0, 45, 77);
		[_qqLoginButton setImage:[UIImage imageNamed:@"qq_login_icon"] forState:UIControlStateNormal];
		[_qqLoginButton addTarget:self
							   action:@selector(loginWithButton:) 
					 forControlEvents:UIControlEventTouchUpInside];
		_qqLoginButton.tag = 3;
	}
	
	return _qqLoginButton;
}

- (UIButton *)mailLoginButton
{
	if (!_mailLoginButton)
	{
		_mailLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
		_mailLoginButton.frame = CGRectMake(0, 0, 45, 77);
		[_mailLoginButton setImage:[UIImage imageNamed:@"mail_login_icon"] forState:UIControlStateNormal];
		[_mailLoginButton addTarget:self
						   action:@selector(loginWithButton:) 
				 forControlEvents:UIControlEventTouchUpInside];
		_mailLoginButton.tag = 4;
	}
	
	return _mailLoginButton;
}


- (void)loginWithButton:(UIButton *)sender
{
	
	if (sender.tag < 4)
	{
		ShareType type = 0;
		switch (sender.tag) {
			case 1:
				type = ShareTypeSinaWeibo;
				break;
			case 2:
				type = ShareTypeWeixiSession;
				break;
			case 3:
				type = ShareTypeQQSpace;
				break;

				break;
			default:
				break;
		}
//		[ShareSDK getUserInfoWithType:type authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
//			if (result) {
//				NSLog(@"授权登陆成功，已获取用户信息");
//				NSString *uid = [userInfo uid];
//				NSString *nickname = [userInfo nickname];
//				NSString *profileImage = [userInfo profileImage];
//				UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:[NSString stringWithFormat:@"授权登陆成功,用户ID:%@,昵称:%@,头像:%@",uid,nickname,profileImage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//				[alert show];
//				NSLog(@"source:%@",[userInfo sourceData]);
//				NSLog(@"uid:%@",[userInfo uid]);
//				
//				
//			}else{
//				NSLog(@"分享失败,错误码:%ld,错误描述%@",(long)[error errorCode],[error errorDescription]);
//				UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Code4App" message:@"授权失败，请看日记错误描述" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//				[alert show];
//			}
//		}];
		
	}
	else
	{
		LoginViewController *loginViewController = [LoginViewController sharedInstance];
		
		[self.navigationController pushViewController:loginViewController animated:YES];
	}
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view. 

    [self.view addSubview:self.backgroundView];
	[self.view addSubview:self.logoView];
	self.logoView.top = 130;
	self.logoView.centerX = self.view.width/2.0;
	
	
	[self.view addSubview:self.weiboLoginButton];
	[self.view addSubview:self.weixinLoginButton];

	[self.view addSubview:self.qqLoginButton];

	[self.view addSubview:self.mailLoginButton];
	
	
	[self layoutSubView];

}


- (void)layoutSubView
{
	CGFloat startx = ([[UIScreen mainScreen] applicationFrame].size.width - 4*self.weiboLoginButton.width - 3*20)/2.0;
	
	self.weiboLoginButton.top = self.logoView.bottom + 150;
	self.weiboLoginButton.left = startx;
	
	self.weixinLoginButton.left = self.weiboLoginButton.right + 20;
	self.weixinLoginButton.top = self.weiboLoginButton.top;
	
	self.qqLoginButton.left = self.weixinLoginButton.right + 20;
	self.qqLoginButton.top = self.weiboLoginButton.top;

	
	self.mailLoginButton.left = self.qqLoginButton.right + 20;
	self.mailLoginButton.top = self.weiboLoginButton.top;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
